package com.hwtx.form.domain.listener;

import com.google.common.collect.Lists;
import com.hwtx.form.domain.FormConstants;
import com.hwtx.form.domain.def.FormDetailExt;
import com.hwtx.form.domain.handle.datamodel.DataModel;
import io.geekidea.boot.framework.exception.BusinessException;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public abstract class AbstractFormDataChangeInterceptor implements FormDataChangeInterceptor {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public void before(FormDataChangeEvent event) {
        before(event.getDataModel(), event.getOperationConditions(), event.getParams());
    }

    private void before(DataModel dataModel, List<FormDetailExt.OperationCondition> operationConditions, Map<String, String[]> params) {
        if (CollectionUtils.isEmpty(operationConditions)) {
            return;
        }
        operationConditions.forEach(op -> {
            FormDetailExt.OperationConfig opConfig = op.getOpConfig();
            if (opConfig == null) {
                return;
            }
            Long count;
            List<Object> param = Lists.newArrayList();
            if (Objects.equals(opConfig.getType(), FormConstants.SqlFunType.COUNT.getSymbol())) {
                Long threshold = Long.valueOf(opConfig.getValue());
                StringBuilder countSql = new StringBuilder();
                countSql.append("select count(*) from ").append(dataModel.getMaster()).append(" where ");
                op.getOpValues().forEach(item -> {
                    if (item.isVariable()) {
                        countSql.append(item.getProperty()).append(item.getOperator()).append(" ?");
                        String[] pValue = params.get(item.getVar());
                        if (pValue != null && pValue.length > 0) {
                            param.add(pValue[0]);
                        }
                    } else {
                        countSql.append(item.getProperty()).append(item.getOperator()).append(item.getValue());
                    }
                });
                count = jdbcTemplate.queryForObject(countSql.toString(), Long.class, param.toArray());
                if (StringUtils.isEmpty(opConfig.getOperator())) {
                    throw new BusinessException("操作符不能为空");
                }
                FormConstants.OperatorType operator = FormConstants.OperatorType.of(opConfig.getOperator());
                if (operator == FormConstants.OperatorType.EQ) {
                    if (!count.equals(threshold)) {
                        throw new BusinessException("操作失败，条件不满足");
                    }
                } else if (operator == FormConstants.OperatorType.NOT_EQ) {
                    if (count.equals(threshold)) {
                        throw new BusinessException("操作失败，条件不满足");
                    }
                }
            }
        });
    }

}
